﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class chengyuStroyManager : MonoBehaviour {

    public UILabel title;
    public UILabel content;
    public UILabel coinLabel;
    public UILabel levelLabel;
    public Dictionary<string, ConfigDataStyle> configDictionary = new Dictionary<string, ConfigDataStyle>();
    private int ConfigFirstId;
    private int ConfigLastId;
    private int GetIntId;
    // Use this for initialization
    void Start()
    {
        //PlayerPrefs.DeleteAll();
        ReadConfig();
    }

    // Update is called once per frame
    void Update()
    {
        coinLabel.text = PlayerPrefs.GetInt( "coin", 0 ).ToString();
    }
    public class ConfigDataStyle
    {
        public string title;
        public string content;
    }
    public void ReadConfig()
    {
        TextAsset binAsset = Resources.Load( "config/chengyuStroy", typeof( TextAsset ) ) as TextAsset;
        //获取配置表的行
        string[] configdata = binAsset.text.Split( '\n' );

        foreach( var data in configdata )
        {
            ConfigDataStyle configdat = new ConfigDataStyle();
            string[] dath = data.ToString().Split( ',' );
            if( dath.Length >= 3 )
            {
                string id = dath[0];
                configdat.title = dath[1];
                configdat.content = dath[2];
                configDictionary.Add( id, configdat );
            }
        }
        ConfigLastId = int.Parse( configdata[configdata.Length - 1].Split( ',' )[0] );  //最后一个ID
        ConfigFirstId = int.Parse( configdata[1].Split( ',' )[0] );  //第一个id
        GetIntId = PlayerPrefs.GetInt( "cysid", ConfigFirstId );
        Init( GetIntId.ToString() );
    }

    public void Init( string i )
    {
        title.text = configDictionary[i].title;
        content.text = configDictionary[i].content;
        coinLabel.text = PlayerPrefs.GetInt( "coin", 0 ).ToString();
        levelLabel.text = PlayerPrefs.GetInt( "cysid", ConfigFirstId ).ToString() + "/" + ConfigLastId;
    }

    public void TopStroy()
    {
        int tap = PlayerPrefs.GetInt( "cysid" );
        Debug.Log( tap );
        if( tap - ConfigFirstId > 0 )
        {
            PlayerPrefs.SetInt( "cysid", PlayerPrefs.GetInt( "cysid", ConfigFirstId ) - 1 );
            Init( PlayerPrefs.GetInt( "cysid" ).ToString() );
        }
    }
    public void DownStroy()
    {
        int Down = PlayerPrefs.GetInt( "cysid" );
        Debug.Log( Down );
        if( Down + 1 <= ConfigLastId )
        {
            PlayerPrefs.SetInt( "cysid", PlayerPrefs.GetInt( "cysid", ConfigFirstId ) + 1 );
            Init( PlayerPrefs.GetInt( "cysid" ).ToString() );
        }
    }
}
